package yf.mall.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.yf.request.SearchParam;
import org.springframework.web.bind.annotation.*;
import yf.mall.vo.MallStoreVo;
import yf.mall.vo.UserVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.entity.UserEntity;
import yf.mall.service.UserService;
import com.yf.response.R;

import javax.servlet.http.HttpServletResponse;



@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表用户
     */
    @RequestMapping("/list")
    @RequiresPermissions("user:list")
    public R list(@RequestBody SearchParam<UserVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<UserVo> userList = userService.queryListVo(params.getEntity());
        initUserInfo(userList.stream().map(UserVo::getUser).collect(Collectors.toList()));
        return R.ok(new PageInfo(userList));
    }

    /**
     * 列表用户
     */
    @RequestMapping("/down")
    @RequiresPermissions("user:list")
    public void down(@RequestBody SearchParam<UserVo> params, HttpServletResponse response) {
        List<UserVo> userList = userService.queryListVo(params.getEntity());
        initUserInfo(userList.stream().map(UserVo::getUser).collect(Collectors.toList()));
        userService.down(userList, response);
    }




//    private void initUserInfo(List<UserEntity> users){
//        Set<Long> userIds = new HashSet<>();
//        users.forEach(user->{
//            if(user.getRid()!=null){
//                userIds.add(user.getRid());
//            }
//            if(user.getFid()!=null){
//                userIds.add(user.getFid());
//            }
//        });
//        List<UserEntity> topUsers = userService.finByUserIds(userIds);
//        if(topUsers.isEmpty())return;
//        Map<Long,UserEntity> userMap = topUsers.stream().collect(Collectors.toMap(UserEntity::getId,Function.identity()));
//        users.forEach(user->{
//            if(user.getRid()!=null){
//                UserEntity rUser = userMap.get(user.getRid());
//                user.setRInfo(rUser.getCode()+" "+rUser.getName());
//            }
//            if(user.getFid()!=null){
//                UserEntity fUser = userMap.get(user.getFid());
//                user.setFInfo(fUser.getCode()+" "+fUser.getName());
//            }
//        });
//    }


    //实时计算RID FID
    private void initUserInfo(List<UserEntity> users) {
        users.forEach(user -> {
            userService.initUserRidFid(user);
//           userService.initUserTeamCount(user);
        });
    }

    /**
     * 查看用户
     */
    @SysLog("查看用户")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("user:info")
    public R info(@PathVariable("id") Long id) {
        UserVo user = userService.getVo(id);

        return R.ok(user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/save")
    @RequiresPermissions("user:save")
    public R save(@RequestBody UserVo userVo) {
        userService.updateVo(userVo);

        return R.ok(userVo);
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    @RequiresPermissions("user:update")
    public R update(@RequestBody UserVo userVo) {
        userService.updateVo(userVo);
        return R.ok(userVo);
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("user:delete")
    public R delete(Long[] ids) {
        userService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有用户
     */
    @SysLog("所有用户")
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<UserEntity> userList = userService.queryList(params);

        return R.ok(userList);
    }

    /***********自定义开始***********/



    @RequestMapping("/child")
    public R child(Long userId) {
        UserEntity user = userService.get(userId);
        userService.initUserTeamCount(user);
        return R.ok().putExtra("childCount", user.getChildCount()).putExtra("teamCount", user.getTeamCount());
    }


}
